﻿using System;

namespace MavlinkPacketViewer
{
    /// <summary>
    /// A mavlink item is a row in the debugger grid
    /// </summary>
    /// <remarks>
    /// This can either be a CRC error block, an unused bytes block or a real packet
    /// Some properties are not relevant to some types so they are nullable
    /// </remarks>
    public class MavlinkItem
    {
        public enum MavlinkItemType
        {
            UnusedBytes,
            FailedCRC,
            Packet
        }

        public MavlinkItem(MavlinkItemType itemType, byte[] bytes, int itemPosition, int streamEventPosition)
        {
            ItemType = itemType;
            Bytes = bytes;
            ItemPosition = itemPosition;
            BytePosition = streamEventPosition;
        }

        public MavlinkItemType ItemType { get; private set; }

        /// <summary>
        /// This exists since the Jib grid cannot cope with enumerated types
        /// </summary>
        public string ItemTypeString { get { return ItemType.ToString(); } }

        /// <summary>
        /// The size of this item in bytes. In the cas of a packet, the total size including the magic and crc
        /// </summary>
        public int Size { get { return Bytes.Length; } }

        /// <summary>
        /// The position of the item in the debugged stream. 
        /// Essentially this will be event position - length
        /// </summary>
        public int BytePosition { get; private set; }

        /// <summary>
        /// The bytes that make up this item. Useful for debugging
        /// </summary>
        public byte[] Bytes { get; private set; }

        /// <summary>
        /// The ordering of this particular item
        /// </summary>
        public int ItemPosition { get; private set; }

        /// <summary>
        /// The MavlinkSequenceNumber
        /// </summary>
        public int SequenceNumber { get; internal set; }

        public int SystemId { get; internal set; }

        public int ComponentId { get; internal set; }
        
        /// <summary>
        /// The Mavlink message class
        /// </summary>
        public string Message { get; internal set; }
        public object MessageObject { get; internal set; }

        public bool HasWarning { get; internal set; }
        public string Warning { get; internal set; }

        public DateTime? TimeStamp { get; internal set; }

        public TimeSpan? TimeStampOffset { get; internal set; }
    }
}
